package org.mclll.xwangplugin.town.database.mapper.manage;

import com.google.common.collect.Lists;
import org.apache.ibatis.session.SqlSession;
import org.jetbrains.annotations.NotNull;
import org.mclll.xwangplugin.datasource.MybatisConfig;
import org.mclll.xwangplugin.town.database.domain.TownApplyDO;
import org.mclll.xwangplugin.town.database.mapper.TownApplyMapper;

import java.util.List;

/**
 * @author: geduo
 * @date: 2023/9/23 18:32
 */
public class TownApplyManage {

    public static List<TownApplyDO> selectList(TownApplyDO param) {
        List<TownApplyDO> ret = Lists.newArrayList();
        try(SqlSession sqlSession = MybatisConfig.SQL_SESSION_FACTORY.openSession()) {
            TownApplyMapper mapper = sqlSession.getMapper(TownApplyMapper.class);
            ret = mapper.selectList(param);
        }
        return ret;
    }

    public static void updateById(TownApplyDO param) {
        try(SqlSession sqlSession = MybatisConfig.SQL_SESSION_FACTORY.openSession()) {
            TownApplyMapper mapper = sqlSession.getMapper(TownApplyMapper.class);
            mapper.updateById(param);
            sqlSession.commit();
        }
    }

    public static void update(TownApplyDO param, @NotNull TownApplyDO where) {
        try(SqlSession sqlSession = MybatisConfig.SQL_SESSION_FACTORY.openSession()) {
            TownApplyMapper mapper = sqlSession.getMapper(TownApplyMapper.class);
            mapper.update(param, where);
            sqlSession.commit();
        }
    }


    public static void insert(TownApplyDO param) {
        try(SqlSession sqlSession = MybatisConfig.SQL_SESSION_FACTORY.openSession()) {
            TownApplyMapper mapper = sqlSession.getMapper(TownApplyMapper.class);
            mapper.insert(param);
            sqlSession.commit();
        }
    }

    public static TownApplyDO selectById(Integer id) {
        try(SqlSession sqlSession = MybatisConfig.SQL_SESSION_FACTORY.openSession()) {
            TownApplyMapper mapper = sqlSession.getMapper(TownApplyMapper.class);
            return mapper.selectById(id);
        }
    }

    public static void createTable() {
        try(SqlSession sqlSession = MybatisConfig.SQL_SESSION_FACTORY.openSession()) {
            TownApplyMapper mapper = sqlSession.getMapper(TownApplyMapper.class);
            mapper.createTable();
            sqlSession.commit();
        }
    }
}
